package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.service.MemeberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员服务业务逻辑处理层
 *
 * @author wangxin
 * @version 1.0
 */
@Service(interfaceClass = MemeberService.class)
@Transactional
public class MemeberServiceImpl implements MemeberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 根据年月获取累计会员数量
     *
     * @param listMonth
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> listMonth) {
        //定义list集合存放会员数量
        List<Integer> memberCount = new ArrayList<>();
        if (listMonth != null && listMonth.size() > 0) {
            for (String yearMonth : listMonth) {
                //2020-02 select count(*) from t_member where regTime <= '2020-02-31'
                yearMonth = yearMonth + "-31";
                Integer count = memberDao.findMemberCountBeforeDate(yearMonth);
                memberCount.add(count);
            }
        }
        return memberCount;
    }

    /**
     * 获取会员性别数量
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getMemberSexCount() {
        List<Map<String, Object>> list = new ArrayList<>();
        Integer maleCount = this.getMemberSexCountBySex("男");
        Integer femaleCount = this.getMemberSexCountBySex("女");
        Map<String, Object> maleMap = new HashMap<>();
        maleMap.put("name","男");
        maleMap.put("value",maleCount);
        Map<String, Object> femaleMap = new HashMap<>();
        femaleMap.put("name","女");
        femaleMap.put("value",femaleCount);
        list.add(maleMap);
        list.add(femaleMap);
        return list;
    }

    /**
     * 按男女性别分别查询性别数量
     *
     * @return
     */
    public Integer getMemberSexCountBySex(String sex) {
        Integer count = null;
        if ("男".equals(sex)) {
            count = memberDao.getMemberSexCountBySex(1);
        } else {
            count = memberDao.getMemberSexCountBySex(2);
        }
        return count;
    }


    @Override
    public List<Map<String,Object>> findMemberCountByAgeGroup() {
        return memberDao.findMemberCountByAgeGroup();
    }
}
